package kr.hkit.androidcomplete02.ch12;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class GridViewEx extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.ch12_gridviewex);
		
		GridView grid = (GridView) findViewById(R.id.ch12_grid);
		
		ImageAdapter adapter = new ImageAdapter(this);
		grid.setAdapter(adapter);
		
		grid.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplication(), position + "번째 그림 선택", Toast.LENGTH_SHORT).show();			}
		});
		
	}
	
	class ImageAdapter extends BaseAdapter{
		private Context mContext;
		int[] picture ={R.drawable.ccdam, R.drawable.soyang2, R.drawable.ududong,
				R.drawable.zipdarigol, R.drawable.dongul};

		public ImageAdapter(Context mContext) {
			super();
			this.mContext = mContext;
		}

		@Override
		public int getCount() {
			return 100;
		}

		@Override
		public Object getItem(int position) {
			return picture[position%5];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			
			if(convertView == null){
				imageView = new ImageView(mContext);
				imageView.setLayoutParams(new GridView.LayoutParams(80, 60));
				imageView.setAdjustViewBounds(false);
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			} else {
				imageView = (ImageView) convertView;
			}
			
			imageView.setImageResource(picture[position%5]);
			
			return imageView;
		}
		
	}
}
